package com.qf.qfwemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.qfleadnewscommons.minio.MinioOperator;
import com.qf.qfleadnewscommons.mvc.ThreadLocalUtils;
import com.qf.qfleadnewsmodel.commons.dtos.PageResponseResult;
import com.qf.qfleadnewsmodel.commons.dtos.ResponseResult;
import com.qf.qfleadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.qfleadnewsmodel.wemedia.dtos.MaterialPageDto;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmMaterial;
import com.qf.qfwemedia.mapper.WmMaterialMapper;
import com.qf.qfwemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinioOperator minioOperator;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Override
    public PageResponseResult pageByCollection(MaterialPageDto materialPageDto) {

        //保障分页参数正确性
        materialPageDto.checkParam();

        //获取当前登录的用户id
        Long uid = ThreadLocalUtils.getUid();

        //0表示查询全部，1表示查询收藏素材
        Integer isCollection = materialPageDto.getIsCollection();

        //设置分页参数
        //注意：mybatisplus的分页查询，一定记得设置分页插件
        IPage<WmMaterial> page = new Page<>(materialPageDto.getPage(),materialPageDto.getSize());

        Wrapper<WmMaterial> qw = Wrappers.lambdaQuery(WmMaterial.class)
                .eq(WmMaterial::getUserId,uid)
                .eq(isCollection==1,WmMaterial::getIsCollection,isCollection)
                .orderByDesc(WmMaterial::getCreatedTime);

        //查询完分页数据后，会将数据封装到page对象并返回
        baseMapper.selectPage(page, qw);


        PageResponseResult result = new PageResponseResult();

        List<WmMaterial> records = page.getRecords();
        //设置分页信息
        result.setCode(200);
        result.setData(records);
        result.setSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setCurrentPage(page.getCurrent());

        return result;
    }

    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws Exception {

        //上传图片 -- 上传到minio（分布式云存储）
        InputStream is = multipartFile.getInputStream();
        if (is != null) {
            String url = minioOperator.uploadImage(is);

            //向数据库的素材表中添加一条记录
            WmMaterial wmMaterial = new WmMaterial();

            Long uid = ThreadLocalUtils.getUid();
            wmMaterial.setUserId(uid.intValue());
            wmMaterial.setUrl(url);
            //0 表示图片 todo: 后续如果兼容视频素材，这类需要动态改变
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setCreatedTime(new Date());

            boolean save = wmMaterialService.save(wmMaterial);

            if (save){
                //添加成功
                return ResponseResult.okResult(wmMaterial);
            }
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.MATERIASL_UPLOAD_FAIL);
    }
}
